<?php
/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */
checkLoginHeader();
checkPermission();
$photos = new photos();

$photos->photosAction();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Images manager</title>
        <style type="text/css" rel="stylesheet">
            .imagesContextAction{
                border:3px silver solid;
                cursor: pointer;
            }
            .error{
                color:red;
                font-weight:bold;
            }
            .success{
                color: green;
                font-weight:bold;
            }
        </style>
        <link type="text/css" rel="stylesheet" href="/admin/themes/default/css/jquery.contextMenu.css" />
        <link rel="stylesheet" href="/admin/themes/default/css/jquery.tabs.css" type="text/css" media="print, projection, screen" />
        <link type="text/css" rel="stylesheet" href="/admin/themes/default/js/uploadify/uploadify.css" />
        <script type="text/javascript" src="/admin/themes/default/js/jquery.min.js"></script>
        <script type="text/javascript" src="/admin/themes/default/js/jquery.MultiFile.pack.js"></script>
        <script type="text/javascript" src="/admin/themes/default/js/jquery.tabs.min.js"></script>
        <script type="text/javascript" src="/admin/themes/default/js/jquery.contextMenu.js"></script>
        <script type="text/javascript" src="/admin/themes/default/js/uploadify/swfobject.js"></script>
        <script type="text/javascript" src="/admin/themes/default/js/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
        <script type="text/javascript">

            $(function() {
                $('#photo-tabs').tabs({ fxShow: { height: 'show', opacity: 'show' }, fxSpeed: 'normal' });
<?php
if($_SESSION['rsMessages']!=''){
    ?>
            setTimeout("closeResultMessages()",5000);
    <?php
}
?>
    });
    function closeResultMessages(){
        $('#rsMessages').hide("slow");
    }
    function returnToParent(src){
        place = '<?php echo $_GET['place'];?>';
        if(place!=''){
            if(place=='description'){
                window.opener.document.getElementById('src').value = src;
                window.opener.document.getElementById('alt').value = src;
            }else if(place=='image'){
                window.opener.document.getElementById('image').value = src;
                window.opener.document.getElementById('imgDisplay').src = src;
            }
            window.close();
        }else{
            return false;
        }
    }
    function del(htmlObj){
        var loc = "<?php echo $config ['domain'].'/'.$config ['admin_section']; ?>/photos?dir=<?php echo $_GET['dir'];?>&place=<?php echo $_GET['place'];?>&photosActionType=deletePhoto&filename="+htmlObj.attr('src');
        if(confirm("Do you want to delete this image?")){
            window.location = loc;
        }
    }
    function imagesContextAction(){
        $(".imagesContextAction").contextMenu({
            menu: 'imagesContextAction'
        }, function(action, el, pos) {
            switch(action){
                case 'select':
                    el.click();
                    break;
                case 'delete':
                    del(el);
                    break;
            }
        });
    }
    $(document).ready(function(){
        imagesContextAction();
        $('#photos').uploadify({
            'uploader'  : '/admin/themes/default/js/uploadify/uploadify.swf',
            'script'    : '/admin/themes/default/js/uploadify/quickpost.php',
            'cancelImg' : '/admin/themes/default/js/uploadify/cancel.png',
            'buttonImg' : '/admin/themes/default/js/uploadify/btnupload.gif',
            'fileExt'   : '*.jpg;*.gif;*.png',
            'multi'     : true,
            'queueSizeLimit' : 100,
            'auto'      : false,
            'removeCompleted' : true,
            'width'      : 95,
            'height'      : 23,
            'scriptData'  : {'duantieubieu':$('input[name=duantieubieu]').val()},
            'onSelect' : function(event,ID,fileObj,data) {
                if(fileObj.name!='' && $('#isUploadImages').val()!=1){
                    $('#isUploadImages').val(1);
                    $('#addPhotosSubmit').attr('disabled', false);
                }
            },
            'onCancel': function(event,ID,fileObj,data) {
                if(data.fileCount==0){
                    $('#isUploadImages').val(0);
                    $('#addPhotosSubmit').attr('disabled', true);
                }
            },
            'onAllComplete' : function(event,data) {
                location.reload(true);
            }
        });
        $('input[name=duantieubieu]').click(function(){
            if($('input[name=duantieubieu]').is(':checked')){
                $('#photos').uploadifySettings('scriptData',{'duantieubieu':1});
            }else{
                $('#photos').uploadifySettings('scriptData',{'duantieubieu':0});
            }
        });
        
    });
    function submitUpload(){
        var isUpload = $('#isUploadImages').val();
        if(isUpload==1){
            $('#photos').uploadifyUpload();
        }
    }
        </script>
    </head>

    <body>
        <ul id="imagesContextAction" class="contextMenu">
            <li class="select"><a href="#select">Select</a></li>
            <li class="delete"><a href="#delete">Delete</a></li>
        </ul>
        <div>
            <div id="photo-tabs">
                <ul>
                    <li><a href="#photo-manage"><span>Manage images</span></a></li>
                    <li><a href="#photo-add"><span>Add images</span></a></li>
                </ul>

                <div id="photo-manage">
                    <div id="rsMessages" style="text-align: left;"><? if(isset($_SESSION['rsMessages'])){echo $_SESSION['rsMessages']; $_SESSION['rsMessages']='';} ?></div>
                    <?php
                    $imageDir = 'uploads/articles/';
                    if($_GET['dir']!=''){
                        $imageDir = $_GET['dir'];
                    }else{
                        $imageDir = 'uploads/articles/'.date('Y').'/'.date("m").'/';
                    }
                    $arrDir = explode('/', $imageDir);
                    for($i=1;$i<count($arrDir)-1;$i++){
                        if($i==1){
                            echo '<a style="margin-right: 12px" href="?dir=uploads/'.$arrDir[$i].'/&place='.$_GET['place'].'">ROOT</a>';
                        }elseif($i==2){
                            echo '>> <a style="margin-right: 12px" href="?dir=uploads/'.$arrDir[$i-1].'/'.$arrDir[$i].'/&place='.$_GET['place'].'">'.$arrDir[$i].'</a>';
                        }elseif($i==3){
                            echo '>> <a style="margin-right: 12px" href="?dir=uploads/'.$arrDir[$i-2].'/'.$arrDir[$i-1].'/'.$arrDir[$i].'/&place='.$_GET['place'].'">'.$arrDir[$i].'</a>';
                        }
                    }

                    echo '<br><br>';

                    $pos = strpos($imageDir, 'thumb');
                    if($pos==FALSE && count($arrDir)!=5 && $_GET['place']=='image') {
                        $photos->listDir($imageDir);
                    }elseif($pos==FALSE && count($arrDir)==5 && $_GET['place']=='image'){
                        $photos->listImages($imageDir.'thumb-137-113/');
                    }elseif($pos==FALSE && count($arrDir)<5 && $_GET['place']=='description'){
                        $photos->listDir($imageDir);
                    }elseif($pos==FALSE && count($arrDir)>4 && $_GET['place']=='description'){
                        $photos->listImagesDescription($imageDir,$imageDir.'thumb-137-113/');
                    }
                    ?>
                    <?php if(intval($_SESSION['pagingPhotos']['totalPages'])>1){?>
                    <script type="text/javascript">
                        var page = <?php echo $_SESSION['pagingPhotos']['page'];?>;
                        var totalPages = <?php echo $_SESSION['pagingPhotos']['totalPages'];?>;

                        function paging(page){
                            var loc = "<?php echo $config ['domain'].'/'.$config ['admin_section']; ?>/photos?dir=<?php echo $_GET['dir'];?>&place=<?php echo $_GET['place'];?>&page="+page;
                            window.location = loc;
                        }
                    </script>
                    <div style="position: absolute; top:45px; left:645px;">
                        <select name="page" style="width:100px;" onchange="paging(this.value);">
                            <script type="text/javascript">
                                for(i=1;i<=totalPages;i++){
                                    if(page==i){
                                        document.write('<option selected="selected" value="'+i+'">Trang '+i+'</option>');
                                    }else{
                                        document.write('<option value="'+i+'">Trang '+i+'</option>');
                                    }
                                }
                            </script>
                        </select>
                        
                    </div>
                    <?php unset ($_SESSION['pagingPhotos']); }?>
                </div>
                
                <div id="photo-add">
                    <div>
                        <form method="post" action="" id="form" name="formAddPhotos" style="padding-top: 10px" enctype="multipart/form-data">
                            <div align="left">
                                <input type="checkbox" name="duantieubieu" value="0" /> Tạo thumb cho dự án tiêu biểu trên trang chủ.
                                <input type="hidden" id="isUploadImages" value=""/>
                                <input type="hidden" id="photosActionType" name="photosActionType" value="addPhotos"/>
                            </div>
                            <div align="left">
                                <div>Select file(s) to upload:</div>
                                <input type="button" value="Upload photos" id="addPhotosSubmit" name="addPhotosSubmit" disabled="true" onclick="submitUpload();" style="float: left;"/>
                                <input type="file" id="photos" style="float: left;"/>
                            </div>
                            <div id="imageList"></div>
                        </form>
                    </div>
                </div>

            </div>
        </div>
    </body>
</html>



